package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.PmsKeyword;
import com.macro.mall.portal.domain.PlantingReadHistory;
import com.macro.mall.portal.service.PlantingReadHistoryService;
import com.macro.mall.portal.service.PmsKeywordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员文章浏览记录管理Controller
 * Created by macro on 2018/8/3.
 */
@Controller
@Api(tags = "MemberReadHistoryController", description = "会员商品浏览记录管理")
@RequestMapping("/plan/readHistory")
public class PlantingReadHistoryController {
    @Autowired
    private PlantingReadHistoryService plantingReadHistoryService;


    @ApiOperation("创建浏览记录")
    @RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public CommonResult create(@RequestBody PlantingReadHistory plantingReadHistory) {
        int count = plantingReadHistoryService.create(plantingReadHistory);
        if (count > 0) {
            return CommonResult.success(count);
        } else if(count == -1){
            return CommonResult.success("");
        }else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("删除浏览记录")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<String> ids) {
        int count = plantingReadHistoryService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }


//    @ApiOperation("清空除浏览记录")
//    @RequestMapping(value = "/clear1", method = RequestMethod.POST)
//    @ResponseBody
//    public CommonResult clear() {
//        plantingReadHistoryService.clear();
//        return CommonResult.success(null);
//    }

    @ApiOperation("获取用户浏览记录")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PlantingReadHistory>> list(@RequestParam Long memberId, @RequestParam Integer type) {
        List<PlantingReadHistory> list = plantingReadHistoryService.list(memberId, type);
        return CommonResult.success(list);
    }

    /**
     * 根据类型清空搜索记录，0是首页的，1是种植技术的
     *
     * @param type
     * @return
     */
    @RequestMapping(value = "/clear", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult clearByType(@RequestParam Integer type) {
        plantingReadHistoryService.clearByType(type);
        return CommonResult.success(null);
    }



}
